package com.wenda.homeschooedu.daoimpl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.wenda.homeschooedu.DB.JDBCConnection;
import com.wenda.homeschooedu.DB.DB.Table;
import com.wenda.homeschooedu.dao.Iclasses;
import com.wenda.homeschooedu.model.Classes;

public class ClassesService implements Iclasses {
	JDBCConnection jdbcConnection;
	Statement statement;

	public ClassesService() {
		jdbcConnection = new JDBCConnection();
		statement = jdbcConnection.getStm();
	}

	public List<Classes> getAllClasses() {
		String sql = String.format(Table.ClassDB.SQL.SELECT, "1=1");
		List<Classes> classes = new ArrayList<Classes>();
		try {
			ResultSet resultSet = statement.executeQuery(sql);
			if (resultSet != null) {
				while (resultSet.next()) {
					Classes cl = new Classes();
					cl.set_id(resultSet.getInt(resultSet.findColumn("class_id")));
					cl.setMaster_id(resultSet.getInt(resultSet
							.findColumn("master_id")));
					cl.setMaster_major(resultSet.getString(resultSet
							.findColumn("master_major")));
					cl.setMaster_name(resultSet.getString(resultSet
							.findColumn("master_name")));
					cl.setMaster_phone(resultSet.getString(resultSet
							.findColumn("master_phone")));
					cl.setMaster_userName(resultSet.getString(resultSet
							.findColumn("master_username")));
					cl.setName(resultSet.getString(resultSet
							.findColumn("class_name")));
					classes.add(cl);
				}
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

		return classes;
	}

	public Classes getClassInfById(int class_id) {
		String sql = String.format(Table.ClassDB.SQL.SELECT, "class._id = "+class_id);
		Classes cl = new Classes();
		
		try {
			ResultSet resultSet = statement.executeQuery(sql);
			if (resultSet!=null) {
				resultSet.next();
				cl.set_id(resultSet.getInt(resultSet.findColumn("class_id")));
				cl.setMaster_id(resultSet.getInt(resultSet
						.findColumn("master_id")));
				cl.setMaster_major(resultSet.getString(resultSet
						.findColumn("master_major")));
				cl.setMaster_name(resultSet.getString(resultSet
						.findColumn("master_name")));
				cl.setMaster_phone(resultSet.getString(resultSet
						.findColumn("master_phone")));
				cl.setMaster_userName(resultSet.getString(resultSet
						.findColumn("master_username")));
				cl.setName(resultSet.getString(resultSet
						.findColumn("class_name")));
			}
		
			
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
		
		
		
		
		
		return cl;
	}

}
